package com.qfedu.rocketmq01.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 千锋Java,为你而生！
 *
 * @Author: Feri
 * @Date:2024/11/14 11:00
 * @Modified:
 */
@RestController
@RequestMapping("api/delay/")
@Tag(name = "延迟消息")
@Slf4j
public class MqDelayController {
    @Resource
    private RocketMQTemplate template;


    @GetMapping("send1")
    public String send(String msg){
        //发送延迟消息 RocketMQ 5.0之后
        //syncSendDelayTimeSeconds 发送延迟消息 设置延迟的秒数
        //syncSendDelayTimeMills 发送定时消息 设置定时的时间戳
//        template.syncSendDelayTimeSeconds("xingmq01",msg+"-"+System.currentTimeMillis(),2);

        //RocketMQ 5.0以下 延迟消息 不支持自定义延迟
        //异步 发送  延迟消息 5个参数
        //同步发送延迟消息 4个参数 1.主题 2.消息对象 3.超时时间 4.延迟等级
        template.syncSend("xingmq01",MessageBuilder.withPayload(msg).build(),
                5000,3);
        log.info("发送延迟消息={}",System.currentTimeMillis());
        return "OK";
    }

    @GetMapping("send2")
    public String send2(){
        template.syncSend("xingmq01",MessageBuilder.withPayload("测试"+System.currentTimeMillis()).build(),5000,3);
        return "OK";
    }
}
